package com.example.abbitmqtest.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {
    public static final String ORDER_DELAY_QUEUE = "order.delay.queue";
    public static final String ORDER_DELAY_EXCHANGE = "order.delay.exchange";
    public static final String ORDER_DELAY_ROUTING_KEY = "order.delay.routingkey";

    // 延迟队列
    @Bean
    public Queue orderDelayQueue() {
        return QueueBuilder.durable(ORDER_DELAY_QUEUE)
                .build();
    }

    // 延迟交换机，使用直连交换机
//    @Bean
//    public DirectExchange orderDelayExchange() {
//        return new DirectExchange(ORDER_DELAY_EXCHANGE);
//    }



    // 延迟交换机，使用延迟消息交换机
    @Bean
    public CustomExchange orderDelayExchange() {
        Map<String, Object> args = new HashMap<>();
        // 设置延迟类型为直连
        args.put("x-delayed-type", "direct");
        // 交换机类型指定为 x-delayed-message
        return new CustomExchange(ORDER_DELAY_EXCHANGE, "x-delayed-message", true, false, args);
    }



    // 绑定延迟队列和交换机
//    @Bean
//    public Binding bindingOrderDelay() {
//        return BindingBuilder.bind(orderDelayQueue())
//                .to(orderDelayExchange())
//                .with(ORDER_DELAY_ROUTING_KEY);
//    }


    // 绑定延迟队列和交换机
    @Bean
    public Binding bindingOrderDelay() {
        return BindingBuilder.bind(orderDelayQueue())
                .to(orderDelayExchange())
                .with(ORDER_DELAY_ROUTING_KEY)
                .noargs();
    }
}